package com.xinlijiankang.web.news;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xinlijiankang.respVO.Result;
import com.xinlijiankang.web.service.OperateService;
import com.xinlijiankang.web.utils.Download;
import com.xinlijiankang.web.utils.Upload;
import com.xinlijiankang.web.utils.UtilFactory;

@Controller
public class NewsController {
	@Resource(name="operateService")
	private OperateService operateService;
	
	/**
	 * 保存一条新闻
	 * 请求方法:saveNews 
	 *     @param title String
	 *     @param author String
	 *     @param article  String
	 *     @param file   File
	 * 返回参数
	 *     @return Result String,String,String
	 * @throws Exception
	 */
	@RequestMapping("saveNews")
	@ResponseBody
	public Result saveNews(@Valid @ModelAttribute News news,BindingResult br,MultipartFile file,HttpServletRequest request) throws Exception{
		Upload upload=UtilFactory.getInstance(Upload.class);
		String filename=upload.photoUploadForSingle(file, request, "upload");
		Result result=new Result();
		
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowtime = dateFormat.format(now); 
		news.setTime(nowtime);
		
		if(filename=="null"){
			news.setFilepath("");
			news.setFilename("");
		}
		else {
			news.setFilepath("/upload/"+filename);
			news.setFilename(filename);
		}
		
		operateService.saveOperate(news);
		
		result.setStatus("200");
		result.setMesg("保存成功");
		result.setData(null);
		return result;
	}
	
	/**
	 * 获取新闻列表
	 * 请求方法:newsList 
	 * 返回参数
	 *     @return Result String,String,List<News>
	 */
	@RequestMapping("newsList")
	@ResponseBody
	public Result listAll() {
		List<News> newsList = operateService.findAll(News.class);
		
		Result result =new Result();
		result.setStatus("200");
		result.setMesg("获取成功");
		result.setData(newsList);		
		return result;
	}
	
	/**
	 * 获取新闻详细信息(查看新闻)
	 * 请求方法:detail
	 *     @param id  Long
	 * 返回参数
	 *     @return Result String,String,News
	 */
	@RequestMapping("detail")
	@ResponseBody
	public Result detail(long id) {
		News news= operateService.findById(News.class, id);
		
		Result result =new Result();
		result.setStatus("200");
		result.setMesg("获取成功");
		result.setData(news);		
		return result;
	}
	
	/**
	 * 附件下载
	 * 请求方法:download 
	 *     @param id Long
	 * @throws Exception
	 * 返回参数
	 *     @return Result String,String,String
	 */
	@RequestMapping("download")
	@ResponseBody
	public void download(HttpServletRequest request,HttpServletResponse response,Long id) throws Exception {
		Download download=UtilFactory.getInstance(Download.class);
		News news= operateService.findById(News.class, id);
		download.download(news.getFilename(), news.getFilepath(), request, response);
		
		Result result =new Result();
		result.setStatus("200");
		result.setMesg("获取成功");
		result.setData(null);
	}
	
}
